import EditorJS from "@editorjs/editorjs";
import {createArticleExport} from "@/pages/note/layout/editor-content/components/ArticleExport";
import {download} from "@/utils/BrowserUtil";
import {extname} from "@/utils/file/FileUtil";

export function handleSuperEditorExport(id: number, editor: EditorJS) {
  createArticleExport(id, [{
    key: 1,
    name: 'Markdown',
    desc: '便于分享',
    extname: 'md'
  }]).then(({type, title}) => {
    if (type === 1) {
      editor.save().then(({blocks}) => {
        const markdown = blocks.map(e => {
          const {type, data} = e;
          console.log(type, data);
          if (type === 'header') {
            return `${"".padStart(data.level, "#")} ${data.text}`
          } else if (type === "paragraph") {
            return data.text;
          } else if (type === 'linkTool') {
            return data.meta.title ? `[${data.meta.title}](${data.link})` : `<${data.link}>`;
          } else if (type === 'alert') {
            return ":::" + data.type + "\n" + data.message + "\n:::";
          } else if (type === 'warning') {
            return ":::warning\n" + data.message + "\n:::";
          } else if (type === 'quote') {
            return "> " + data.text + "\n> 作者：" + data.caption;
          } else if (type === "list") {
            const {items, style} = data;
            const renderList = (items: any[], prefix: string): string => {
              return items.map((e: any, index) => {
                const {content, items, meta} = e;
                if (items && items.length > 0) {
                  return renderList(items, prefix + '  ')
                } else if (style === 'ordered') {
                  return `${index + 1}. ${content}`
                } else if (style === 'checklist') {
                  return `- [${meta.checked ? 'x' : ' '}]${content}`
                }
                return `- ${content}`
              }).join('\n');
            }
            return renderList(items, '');
          } else if (type === 'Delimiter') {
            return "---"
          } else if (type === 'Table') {
            const content = data.content as Array<Array<string>>;
            if (content.length === 0) return '';
            const widths = content[0].map(e => e.length);
            content.forEach(e => {
              e.forEach((v, i) => {
                widths[i] = Math.max(widths[i], v.length)
              })
            });
            const head = content[0].map((c, i) => c.padEnd(widths[i], ' ')).join('|');
            const splits = widths.map(e => "-".repeat(e)).join('|');
            let body = content.slice(1).map(r => r.map(((c, i) => c.padEnd(widths[i], ' '))).join('|')).join("\n");
            return "|" + [head, splits, body].join('|\n|') + "|";
          } else if (type === 'CodeTool') {
            return "```" + extname(data.filename) + "\n" + data.html + "\n```";
          } else if (type === 'MindMapTool') {
            return "> 思维导图暂不支持导出\n\n```json\n" + JSON.stringify(data.root) + "\n```";
          } else if (type === 'KanbanTool') {
            return "> 看板暂不支持导出\n\n```json\n" + JSON.stringify(data) + "\n```";
          } else if (type === 'EncryptText') {
            return "> 加密笔记暂不支持导出";
          }
          return JSON.stringify(data);
        }).join('\n\n');
        download(markdown, title + '.md', 'text/plain')
      })
    }
  })
}